package View;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.freixas.jcalendar.JCalendar;
import org.freixas.jcalendar.JCalendarCombo;

import Events.Alarm;
import Events.Appointment;
import Events.EventParams;
import Events.GenericEvent;
import Events.Task;


public class RecurrenceFrame implements ActionListener 
{
	public JFrame frame;
	public GenericEvent evento;
	public JLabel lb2;
	public JComboBox starttime,endtime;
	public JCalendarCombo fecha, fecha2;
	public JTextField num,num2,every1,every2;
	public int opcselect = 0;
	public boolean[] statusDate, weekpatt;
	public Vector<JButton> enddate;
	private String dir = "images/";
	public String[] pattern = new String[4],dayWeek = new String[7], monthday = new String[5], months = new String[12] ;
	public Boolean [] status = new Boolean[4];
	public int view=0;
	public JComboBox combodia, combotipo;
	public Vector<JButton> btpatter = new Vector<JButton>(), btmove = new Vector<JButton>();
	
	
	
	public RecurrenceFrame(GenericEvent event)
	{
		Alarm a = new Alarm();
		Appointment app = new Appointment();
		Task tk = new Task();	
		evento = event;
		
		if(evento.getType()==0)
		{
			app= (Appointment)evento;
			view = app.getRecurrence().getType();
		}
		else if(evento.getType()==1)
		{
			a= (Alarm)evento;
			
			view = a.getRecurrence().getType();
		}
		else
		{
			tk=(Task)evento;
			view = tk.getRecurrence().getType();
		}
		
		pattern[0]="Daily";
		pattern[1]="Weekly";
		pattern[2]="Monthly";
		pattern[3]="Yearly";
		
		dayWeek[0]="Sunday";
		dayWeek[1]="Monday";
		dayWeek[2]="Tuesday";
		dayWeek[3]="Wednesday";
		dayWeek[4]="Thursday";
		dayWeek[5]="Friday";
		dayWeek[6]="Saturday";	
		
		months[0]="January";
		months[1]="February";
		months[2]="March";
		months[3]="April";
		months[4]="May";
		months[5]="June";
		months[6]="July";
		months[7]="August";
		months[8]="September";
		months[9]="October";
		months[10]="November";
		months[11]="December";
		
		for(int i=0;i<status.length;i++)
		{
			if(event.getType()==0)
			{
				if(app.getRecurrence().getType()==i)
					status[i]=true;
				else
					status[i]=false;
			}
			else if(event.getType()==1)
			{
							
				if(a.getRecurrence().getType()==i)
					status[i]=true;
				else
					status[i]=false;
			}
			else
			{
				if(tk.getRecurrence().getType()==i)
					status[i]=true;
				else
					status[i]=false;
			}
		}
		
		statusDate = new boolean[3];
		statusDate[0]=false;
		statusDate[1]=false;
		statusDate[2]=false;
		
		weekpatt = new boolean[7];
		weekpatt[0]=true;
		weekpatt[1]=false;
		weekpatt[2]=false;
		weekpatt[3]=false;
		weekpatt[4]=false;
		weekpatt[5]=false;
		weekpatt[6]=false;
		
		monthday[0]="first";
		monthday[1]="second";
		monthday[2]="third";
		monthday[3]="fourth";
		monthday[4]="last";
		
		enddate = new Vector<JButton>();
		
	
		createFrame();
	}
	
	public void createFrame()
	{
		frame= new JFrame("Recurrence");
		frame.setSize(420,620);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		JLabel fondo = new JLabel();
		fondo.setPreferredSize(new Dimension(400,620));
		fondo.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		fondo.setIcon(new ImageIcon(dir+"fondorecurrencia.png"));
		
		JLabel lb1 = new JLabel();
		lb1.setPreferredSize(new Dimension(250,100));
		lb1.setLayout(new FlowLayout(FlowLayout.LEFT,15,10));
		
		JLabel lb = new JLabel();
		lb.setPreferredSize(new Dimension(400,250));
		lb.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		JLabel lb3 = new JLabel();
		lb3.setLayout(new FlowLayout(FlowLayout.LEFT));
		lb3.setPreferredSize(new Dimension(400,150));
		
		JLabel lb4 = new JLabel();
		lb4.setPreferredSize(new Dimension(400,70));
		lb4.setLayout(new FlowLayout(FlowLayout.RIGHT,10,20));
		
		createAppointmentTime(lb1);
		createRecurrencePattern(lb);
		createRangeRecurrence(lb3);
		createMenu(lb4);
		
		fondo.add(lb1);
		fondo.add(lb);
		fondo.add(lb3);
		fondo.add(lb4);
		
		frame.add(fondo);
		frame.setVisible(true);
	}
	
	public void createAppointmentTime(JLabel lb)
	{
		Alarm a;
		Appointment app;
		Task tk;
		Date d, d2;
		
		if(evento.getType()==0)
		{
			app= (Appointment)evento;
			d=app.getRecurrence().getStart();
			d2=app.getRecurrence().getEnd();
		}
		else if(evento.getType()==1)
		{
			a= (Alarm)evento;			
			d=a.getRecurrence().getStart();
			d2=a.getRecurrence().getEnd();	
		}
		else
		{
			tk=(Task)evento;
			d=tk.getRecurrence().getStart();
			d2=tk.getRecurrence().getEnd();
		}
		
		JLabel n= new JLabel();
		n.setPreferredSize(new Dimension(400,10));
		
		JLabel txt1 = new JLabel("Start : ");
		txt1.setPreferredSize(new Dimension(70,20));
		txt1.setFont(new Font("Corbel", Font.PLAIN, 14));
		
		
		starttime = new JComboBox();	
		starttime.setPreferredSize(new Dimension(100,25));
		starttime.setFont(new Font("Corbel", Font.PLAIN, 20));
		starttime.setBackground(Color.white);
		
		JLabel txt2 = new JLabel("End : ");
		txt2.setPreferredSize(new Dimension(70,20));
		txt2.setFont(new Font("Corbel", Font.PLAIN, 14));	
		
		endtime = new JComboBox();
		endtime.setPreferredSize(new Dimension(100,25));
		endtime.setFont(new Font("Corbel", Font.PLAIN, 20));
		endtime.setBackground(Color.white);
		
		
		for(int i=0; i<24;i++)
		{
			starttime.addItem(i+":00");
			starttime.addItem(i+":30");
			
			endtime.addItem(i+":00");
			endtime.addItem(i+":30");
		}
		
		starttime.setSelectedItem(d.getHours()+":"+d.getMinutes());
		endtime.setSelectedItem(d2.getHours()+":"+d.getMinutes());
	
		
		lb.add(n);
		lb.add(txt1);
		lb.add(starttime);
		lb.add(txt2);
		lb.add(endtime);
		
	}
	
	
	public void createRecurrencePattern(JLabel lb)
	{
		btpatter.removeAllElements();
		JLabel lizq = new JLabel();
		lizq.setLayout(new FlowLayout(FlowLayout.CENTER,7,20));
		lizq.setPreferredSize(new Dimension(100,250));
		
		for(int i=0;i<pattern.length;i++)
		{
			JButton check = new JButton();
			check.setPreferredSize(new Dimension(20,20));
			check.setContentAreaFilled(false);
			check.addActionListener(this);
			check.setActionCommand("STP_"+i);
			if(status[i])
				check.setIcon(new ImageIcon(dir+"select.png"));
			else
				check.setIcon(new ImageIcon(dir+"noselect.png"));
			
			btpatter.add(check);
			JLabel txtpatter = new JLabel(pattern[i]);
			txtpatter.setPreferredSize(new Dimension(60,30));
			txtpatter.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			lizq.add(check);
			lizq.add(txtpatter);
			
		}
		
		
		
		lb2 = new JLabel();
		lb2.setPreferredSize(new Dimension(290,250));
		lb2.setLayout(new FlowLayout(FlowLayout.LEFT,10,15));
		
		lb.add(lizq);
		lb.add(lb2);
		lb.updateUI();
		updatePatter();
	}
	
	public void createRangeRecurrence(JLabel lb3)
	{
		
		Alarm a;
		Appointment app;
		Task tk;
		Date d = new Date(), d2= new Date();
		int in=0;
		int numOcurr=0;	
		
		if(evento.getType()==0)
		{
					
			app= (Appointment)evento;
			d=app.getRecurrence().getStartDate();
			if(app.getRecurrence().getEndDate()!=null)
			{
				d2=app.getRecurrence().getEndDate();
				in=2;
				statusDate[0]=false;
				statusDate[1]=false;
				statusDate[2]=true;
			}
			else
			{
				if(app.getRecurrence().getRecurrenceNum()!=0)
				{
					in=1;
					numOcurr=app.getRecurrence().getRecurrenceNum();
					statusDate[0]=false;
					statusDate[1]=true;					
					statusDate[2]=false;
				}
				else
				{
					in=0;
					statusDate[0]=true;
					statusDate[1]=false;
					statusDate[2]=false;
				}
			}
			
		}
		else if(evento.getType()==1)
		{
			a= (Alarm)evento;
			d=a.getRecurrence().getStartDate();
			if(a.getRecurrence().getEndDate()!=null)
			{
				d2=a.getRecurrence().getEndDate();
				in=2;
				statusDate[0]=false;
				statusDate[1]=false;
				statusDate[2]=true;
			}
			else
			{
				if(a.getRecurrence().getRecurrenceNum()!=0)
				{
					in=1;
					numOcurr=a.getRecurrence().getRecurrenceNum();
					statusDate[0]=false;
					statusDate[1]=true;					
					statusDate[2]=false;
				}
				else
				{
					in=0;
					statusDate[0]=true;
					statusDate[1]=false;
					statusDate[2]=false;
				}
			}		
		}
		else
		{
			tk=(Task)evento;
			d=tk.getRecurrence().getStartDate();
			if(tk.getRecurrence().getEndDate()!=null)
			{
				d2=tk.getRecurrence().getEndDate();
				in=2;
				statusDate[0]=false;
				statusDate[1]=false;
				statusDate[2]=true;
			}
			else
			{
				if(tk.getRecurrence().getRecurrenceNum()!=0)
				{
					in=1;
					numOcurr=tk.getRecurrence().getRecurrenceNum();
					statusDate[0]=false;
					statusDate[1]=true;					
					statusDate[2]=false;
				}
				else
				{
					in=0;
					statusDate[0]=true;
					statusDate[1]=false;
					statusDate[2]=false;
				}
			}
		}
		
		
		JLabel e = new JLabel();
		e.setPreferredSize(new Dimension(400,20));
		
		JLabel txt = new JLabel("Start :");
		txt.setPreferredSize(new Dimension(40,20));
		txt.setFont(new Font("Corbel", Font.PLAIN, 14));
		
		fecha = new JCalendarCombo();
		fecha.setPreferredSize(new Dimension(170,25));
		
		JLabel e2 = new JLabel();
		e2.setPreferredSize(new Dimension(170,20));
		
		
		lb3.add(e);
		lb3.add(txt);
		lb3.add(fecha);
		lb3.add(e2);
		
		for(int i=0; i<3; i++)
		{
			JLabel e2a = new JLabel();

			if(i==0)
				e2a.setPreferredSize(new Dimension(270,20));
			else if(i==1)
				e2a.setPreferredSize(new Dimension(130,20));
			else
				e2a.setPreferredSize(new Dimension(20,20));
			
			JLabel txt1 = new JLabel();
			if(i==0)
				txt1.setText("No end date");
			else if(i==1)
				txt1.setText("End after : ");
			else
				txt1.setText("End by :");
			
			txt1.setPreferredSize(new Dimension(90,20));
			txt1.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			JButton bt = new JButton();
			bt.setPreferredSize(new Dimension(20,20));
			bt.addActionListener(this);
			bt.setActionCommand("ST_"+i);
			bt.setContentAreaFilled(false);
			
			num2 = new JTextField();
			num2.setPreferredSize(new Dimension(40,25));
			num2.setFont(new Font("Corbel", Font.PLAIN, 16));
			
			JLabel txt2 = new JLabel("  ocurrences");
			txt2.setPreferredSize(new Dimension(80,20));
			txt2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			fecha2 = new JCalendarCombo();
			fecha2.setPreferredSize(new Dimension(170,25));
			
			enddate.add(bt);
			
			if(statusDate[i])
				enddate.elementAt(i).setIcon(new ImageIcon(dir+"select.png"));
			else
				enddate.elementAt(i).setIcon(new ImageIcon(dir+"noselect.png"));
			
			lb3.add(enddate.elementAt(i));			
			lb3.add(txt1);
			if(i==1)
			{
				lb3.add(num2);
				lb3.add(txt2);
			}
			else if(i==2)
				lb3.add(fecha2);
			lb3.add(e2a);
			
		}
		if(in==2)
			fecha2.setDate(d2);
		else if(in==1)
			num2.setText(""+numOcurr);
			
		lb3.updateUI();
	}
	
	public void createMenu(JLabel lb)
	{
		JButton ok = new JButton();
		ok.setContentAreaFilled(false);
		ok.setIcon(new ImageIcon(dir+"Rok.png"));
		ok.setPreferredSize(new Dimension(100,40));
		ok.addActionListener(this);
		ok.setActionCommand("OK");
		ok.setFocusPainted(false);
		ok.setBorderPainted(false);
		
		JButton ok2 = new JButton();
		ok2.setContentAreaFilled(false);
		ok2.setFocusPainted(false);
		ok2.setIcon(new ImageIcon(dir+"Rcancel.png"));
		ok2.setPreferredSize(new Dimension(100,40));
		ok2.addActionListener(this);
		ok2.setBorderPainted(false);
		ok2.setActionCommand("CANCEL");
		
		lb.add(ok2);
		lb.add(ok);
		
	}
	
	public void changePatter(int t)
	{
		lb2.removeAll();
		btmove.removeAllElements();
		view = t;
		
		Alarm a;
		Appointment app;
		Task tk;
		
		JLabel va= new JLabel();
		va.setPreferredSize(new Dimension(300,5));
		lb2.add(va);
		((EventParams)evento).getRecurrence().setType(t);
		if(t==0)
		{
			String number="";
			int temp;
			if(evento.getType()==0)
			{
				app= (Appointment)evento;
				if(!app.getRecurrence().getParamsType().get("Every").toString().equals(""))
					temp = Integer.parseInt(app.getRecurrence().getParamsType().get("Every").toString());
				else
					temp=0;
				if(temp!=0)
				{
					number=(String)app.getRecurrence().getParamsType().get("Every");
					opcselect =0;
				}
			}
			else if(evento.getType()==1)
			{
				a= (Alarm)evento;	
				temp = ((Number)a.getRecurrence().getParamsType().get("Every")).intValue();
				if(temp!=0)
				{
					number=(String)a.getRecurrence().getParamsType().get("Every");
					opcselect =0;
				}
			}
			else
			{
				tk=(Task)evento;
				temp = ((Number)tk.getRecurrence().getParamsType().get("Every")).intValue();
				if(temp!=0)
				{
					number=(String)tk.getRecurrence().getParamsType().get("Every");
					opcselect =0;
				}
			}
			
			
			JButton bt = new JButton();
			bt.setPreferredSize(new Dimension(20,20));
			if(opcselect==0)
				bt.setIcon(new ImageIcon(dir+"select.png"));
			else
				bt.setIcon(new ImageIcon(dir+"noselect.png"));
			bt.setContentAreaFilled(false);
			bt.addActionListener(this);
			bt.setActionCommand("OPC_0");
			btmove.add(bt);
			
			JLabel txt = new JLabel("Every ");
			txt.setPreferredSize(new Dimension(60,20));
			txt.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			every1 = new JTextField();
			every1.setPreferredSize(new Dimension(50,25));
			if(opcselect==0)
				every1.setText(number);
			
			JLabel txt2 = new JLabel("day(s)");
			txt2.setPreferredSize(new Dimension(50,20));
			txt2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			JButton bt1 = new JButton();
			bt1.setPreferredSize(new Dimension(20,20));
			if(opcselect==1)
				bt1.setIcon(new ImageIcon(dir+"select.png"));
			else
				bt1.setIcon(new ImageIcon(dir+"noselect.png"));
			bt1.addActionListener(this);
			bt1.setActionCommand("OPC_1");
			bt1.setContentAreaFilled(false);
			btmove.add(bt1);
			
			JLabel txt3 = new JLabel("Every weekday ");
			txt3.setPreferredSize(new Dimension(120,20));
			txt3.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			JLabel e = new JLabel();
			e.setPreferredSize(new Dimension(50,20));
			
			lb2.add(bt);
			lb2.add(txt);
			lb2.add(every1);
			lb2.add(txt2);
			lb2.add(e);
			lb2.add(bt1);
			lb2.add(txt3);			
			
		}
		else if(t==1)
		{
			
			String number="";
			
			
			if(evento.getType()==0)
			{
				app= (Appointment)evento;
				number=app.getRecurrence().getParamsType().get("Recurevery").toString();
				weekpatt[0]=(Boolean)app.getRecurrence().getParamsType().get("Sun");
				weekpatt[1]=(Boolean)app.getRecurrence().getParamsType().get("Mon");
				weekpatt[2]=(Boolean)app.getRecurrence().getParamsType().get("Tue");
				weekpatt[3]=(Boolean)app.getRecurrence().getParamsType().get("Wed");
				weekpatt[4]=(Boolean)app.getRecurrence().getParamsType().get("Thu");
				weekpatt[5]=(Boolean)app.getRecurrence().getParamsType().get("Fri");
				weekpatt[6]=(Boolean)app.getRecurrence().getParamsType().get("Sat");
				
				
			}
			else if(evento.getType()==1)
			{
				a= (Alarm)evento;	
				number=a.getRecurrence().getParamsType().get("Recurevery").toString();
				weekpatt[0]=(Boolean)a.getRecurrence().getParamsType().get("Sun");
				weekpatt[1]=(Boolean)a.getRecurrence().getParamsType().get("Mon");
				weekpatt[2]=(Boolean)a.getRecurrence().getParamsType().get("Tue");
				weekpatt[3]=(Boolean)a.getRecurrence().getParamsType().get("Wed");
				weekpatt[4]=(Boolean)a.getRecurrence().getParamsType().get("Thu");
				weekpatt[5]=(Boolean)a.getRecurrence().getParamsType().get("Fri");
				weekpatt[6]=(Boolean)a.getRecurrence().getParamsType().get("Sat");
			}
			else
			{
				tk=(Task)evento;
				number=tk.getRecurrence().getParamsType().get("Recurevery").toString();
				weekpatt[0]=(Boolean)tk.getRecurrence().getParamsType().get("Sun");
				weekpatt[1]=(Boolean)tk.getRecurrence().getParamsType().get("Mon");
				weekpatt[2]=(Boolean)tk.getRecurrence().getParamsType().get("Tue");
				weekpatt[3]=(Boolean)tk.getRecurrence().getParamsType().get("Wed");
				weekpatt[4]=(Boolean)tk.getRecurrence().getParamsType().get("Thu");
				weekpatt[5]=(Boolean)tk.getRecurrence().getParamsType().get("Fri");
				weekpatt[6]=(Boolean)tk.getRecurrence().getParamsType().get("Sat");
			}
			
			
			JLabel l = new JLabel("Recur every");
			l.setPreferredSize(new Dimension(80,20));
			l.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			every1 = new JTextField();
			every1.setText(number);
			every1.setPreferredSize(new Dimension(50,25));
			
			JLabel l2 = new JLabel("week(s) on : ");
			l2.setPreferredSize(new Dimension(130,20));
			l2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			lb2.add(l);
			lb2.add(every1);
			lb2.add(l2);
			
			for(int i=0; i<dayWeek.length;i++)
			{
				JButton bt1 = new JButton();
				bt1.setPreferredSize(new Dimension(20,20));
				if(weekpatt[i]==true)
					bt1.setIcon(new ImageIcon(dir+"select.png"));
				else
					bt1.setIcon(new ImageIcon(dir+"noselect.png"));
				bt1.addActionListener(this);
				bt1.setActionCommand("OPC_"+i);
				bt1.setContentAreaFilled(false);
				
				JLabel in = new JLabel(dayWeek[i]);
				in.setPreferredSize(new Dimension(100,20));
				in.setFont(new Font("Corbel", Font.PLAIN, 14));
				
				btmove.add(bt1);
				lb2.add(btmove.elementAt(i));
				lb2.add(in);
			}			
		}
		else if (t==2)
		{
//			((EventParams)evento).getRecurrence().setType(2);
			String number1="", number2="";
			int temp;
			if(evento.getType()==0)
			{
				app= (Appointment)evento;
				temp = ((Number)app.getRecurrence().getParamsType().get("day")).intValue();
				if(temp!=0)
				{
					number1=app.getRecurrence().getParamsType().get("day").toString();
					number2=app.getRecurrence().getParamsType().get("ofEvery").toString();
					opcselect=0;
				}
				else
				{
					number1=app.getRecurrence().getParamsType().get("every").toString();
					number2=app.getRecurrence().getParamsType().get("dayofweek").toString();
					opcselect=1;
				}
			}
			else if(evento.getType()==1)
			{
				a= (Alarm)evento;	
				temp = ((Number)a.getRecurrence().getParamsType().get("day")).intValue();
				if(temp!=0)
				{
					number1=a.getRecurrence().getParamsType().get("day").toString();
					number2=a.getRecurrence().getParamsType().get("ofEvery").toString();
					opcselect=0;
				}
				else
				{
					number1=a.getRecurrence().getParamsType().get("every").toString();
					number2=a.getRecurrence().getParamsType().get("dayofweek").toString();
					opcselect=1;
				}
			}
			else
			{
				tk=(Task)evento;
				temp = ((Number)tk.getRecurrence().getParamsType().get("day")).intValue();
				if(temp!=0)
				{
					number1=tk.getRecurrence().getParamsType().get("day").toString();
					number2=tk.getRecurrence().getParamsType().get("ofEvery").toString();
					opcselect=0;
				}
				else
				{
					number1=tk.getRecurrence().getParamsType().get("every").toString();
					number2=tk.getRecurrence().getParamsType().get("dayofweek").toString();
					opcselect=1;
				}
			}
			
			
			
			JButton bt = new JButton();
			bt.setPreferredSize(new Dimension(20,20));
			if(opcselect==0)
				bt.setIcon(new ImageIcon(dir+"select.png"));
			else
				bt.setIcon(new ImageIcon(dir+"noselect.png"));
			bt.setContentAreaFilled(false);
			bt.addActionListener(this);
			bt.setActionCommand("OPC_0");
			btmove.add(bt);
			
			JLabel txt = new JLabel("Day");
			txt.setPreferredSize(new Dimension(30,20));
			txt.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			every1 = new JTextField();
			if(opcselect==0)
				every1.setText(number1);
			every1.setPreferredSize(new Dimension(30,25));
			
			JLabel txt2 = new JLabel("of every");
			txt2.setPreferredSize(new Dimension(50,20));
			txt2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			every2 = new JTextField();
			if(opcselect==0)
				every2.setText(number2);
			every2.setPreferredSize(new Dimension(30,25));
			
			JLabel txt3 = new JLabel("month(s)");
			txt3.setPreferredSize(new Dimension(60,20));
			txt3.setFont(new Font("Corbel", Font.PLAIN, 14));		
			
			JButton bt2 = new JButton();
			bt2.setPreferredSize(new Dimension(20,20));
			if(opcselect==1)
				bt2.setIcon(new ImageIcon(dir+"select.png"));
			else
				bt2.setIcon(new ImageIcon(dir+"noselect.png"));
			bt2.setContentAreaFilled(false);
			bt2.addActionListener(this);
			bt2.setActionCommand("OPC_1");
			btmove.add(bt2);
			
			JLabel t2 = new JLabel("The");
			t2.setPreferredSize(new Dimension(30,20));
			t2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			combodia = new JComboBox();
			combodia.setPreferredSize(new Dimension(80,25));
			combodia.setFont(new Font("Corbel", Font.PLAIN, 14));
			for(int i=0;i<monthday.length;i++)
				combodia.addItem(monthday[i]);
			
			combotipo = new JComboBox();
			combotipo.setPreferredSize(new Dimension(120,25));
			combotipo.setFont(new Font("Corbel", Font.PLAIN, 14));
			for(int j=0;j<dayWeek.length;j++)
				combotipo.addItem(dayWeek[j]);
			
			
			if(opcselect==1)
			{
				combodia.setSelectedItem(number1);
				combotipo.setSelectedItem(number2);
			}
			
			
			lb2.add(bt);
			lb2.add(txt);
			lb2.add(every1);
			lb2.add(txt2);
			lb2.add(every2);
			lb2.add(txt3);
			lb2.add(bt2);
			lb2.add(t2);
			lb2.add(combodia);
			lb2.add(combotipo);
		}
		else
		{
			
			String number="", combo="", number2="";
			if(evento.getType()==0)
			{
				app= (Appointment)evento;
				number = app.getRecurrence().getParamsType().get("every").toString();
				combo =app.getRecurrence().getParamsType().get("onMonth").toString();
				number2 =(String)app.getRecurrence().getParamsType().get("dayofmonth").toString();
				
			}
			else if(evento.getType()==1)
			{
				a= (Alarm)evento;	
				number = (String)a.getRecurrence().getParamsType().get("every").toString();
				combo =(String)a.getRecurrence().getParamsType().get("onMonth").toString();
				number2 =(String)a.getRecurrence().getParamsType().get("dayofmonth").toString();
			}
			else
			{
				tk=(Task)evento;
				number = (String)tk.getRecurrence().getParamsType().get("every").toString();
				combo =(String)tk.getRecurrence().getParamsType().get("onMonth").toString();
				number2 =(String)tk.getRecurrence().getParamsType().get("dayofmonth").toString();
			}
			
			
			JLabel txt = new JLabel("Recur every");
			txt.setPreferredSize(new Dimension(80,20));
			txt.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			every1 = new JTextField();
			every1.setText(number);
			every1.setPreferredSize(new Dimension(30,25));
			
			JLabel txt2 = new JLabel("year(s)");
			txt2.setPreferredSize(new Dimension(150,20));
			txt2.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			JLabel txt3 = new JLabel("On : ");
			txt3.setPreferredSize(new Dimension(30,20));
			txt3.setFont(new Font("Corbel", Font.PLAIN, 14));
			
			combodia = new JComboBox();
			combodia.setPreferredSize(new Dimension(150,25));
			combodia.setFont(new Font("Corbel", Font.PLAIN, 14));
			for(int i=0;i<months.length;i++)
				combodia.addItem(months[i]);
			
			combodia.setSelectedItem(combo);
			
			every2 = new JTextField();
			every2.setText(number2);
			every2.setPreferredSize(new Dimension(40,25));			
			
			lb2.add(txt);
			lb2.add(every1);
			lb2.add(txt2);
			lb2.add(txt3);
			lb2.add(combodia);
			lb2.add(every2);
		}	
		
		lb2.updateUI();
	}
	
	public void updatePatter()
	{
		for(int i=0;i<btpatter.size();i++)
		{
			if(status[i])
			{
				btpatter.elementAt(i).setIcon(new ImageIcon(dir+"select.png"));
				changePatter(i);
			}
			else
				btpatter.elementAt(i).setIcon(new ImageIcon(dir+"noselect.png"));
			btpatter.elementAt(i).updateUI();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		System.out.println(arg0.getActionCommand());
		
		if(arg0.getActionCommand().equals("OK"))
		{
			
			if(evento.getType()==0)
			{
				Appointment app = (Appointment)evento;
				Date s = new Date();
				String st = starttime.getSelectedItem().toString();
				String[] tiempos = st.split(":");
				s.setHours(Integer.parseInt(tiempos[0]));
				s.setMinutes(Integer.parseInt(tiempos[1]));
				
				Date s2 = new Date();
				String st2 = endtime.getSelectedItem().toString();
				String[] tiempos2 = st2.split(":");
				s2.setHours(Integer.parseInt(tiempos2[0]));
				s2.setMinutes(Integer.parseInt(tiempos2[1]));
				
				
				
				app.getRecurrence().setStart(s);
				app.getRecurrence().setEnd(s2);
				app.getRecurrence().setType(view);
				if(view==0)
				{
					if(opcselect==0)
					{
						if(!every1.getText().equals(""))
								app.getRecurrence().getParamsType().put("Every",Integer.parseInt(every1.getText()));
						else
							app.getRecurrence().getParamsType().put("Every","");
						app.getRecurrence().getParamsType().put("EveryWeekday",false);
					}
					else
					{
						app.getRecurrence().getParamsType().put("Every",false);
						app.getRecurrence().getParamsType().put("EveryWeekday",1);
					}
				}
				else if(view==1)
				{
					app.getRecurrence().getParamsType().put("Recurevery",Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("Sun", weekpatt[0]);
					app.getRecurrence().getParamsType().put("Mon", weekpatt[1]);
					app.getRecurrence().getParamsType().put("Tue", weekpatt[2]);
					app.getRecurrence().getParamsType().put("Wed", weekpatt[3]);
					app.getRecurrence().getParamsType().put("Thu", weekpatt[4]);
					app.getRecurrence().getParamsType().put("Fri", weekpatt[5]);
					app.getRecurrence().getParamsType().put("Sat", weekpatt[6]);
				}
				else if(view==2)
				{
					if(opcselect==0)
					{
						app.getRecurrence().getParamsType().put("day", Integer.parseInt(every1.getText()));
						app.getRecurrence().getParamsType().put("ofEvery", Integer.parseInt(every2.getText()));
						app.getRecurrence().getParamsType().put("The", false);
					}
					else
					{						
						app.getRecurrence().getParamsType().put("The", true);
						app.getRecurrence().getParamsType().put("every", combodia.getSelectedItem());
						app.getRecurrence().getParamsType().put("dayofweek", combotipo.getSelectedItem());
					}
				}
				else
				{
					app.getRecurrence().getParamsType().put("every", Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("onMonth", combodia.getSelectedItem());
					app.getRecurrence().getParamsType().put("dayofmonth", Integer.parseInt(every2.getText()));
				}
				
				app.getRecurrence().setStartDate(fecha.getDate());
				if(statusDate[0])
					app.getRecurrence().setEndDate(null);
				else if(statusDate[1])
				{
					app.getRecurrence().setRecurrenceNum(Integer.parseInt(num2.getText()));
					app.getRecurrence().setEndDate(null);
				}
				else
					app.getRecurrence().setEndDate(fecha2.getDate());
				
					
				
			}
			else if(evento.getType()==1)
			{
				Alarm app = (Alarm)evento;
				Date s = new Date();
				String st = starttime.getSelectedItem().toString();
				String[] tiempos = st.split(":");
				s.setHours(Integer.parseInt(tiempos[0]));
				s.setMinutes(Integer.parseInt(tiempos[1]));
				
				Date s2 = new Date();
				String st2 = endtime.getSelectedItem().toString();
				String[] tiempos2 = st2.split(":");
				s2.setHours(Integer.parseInt(tiempos2[0]));
				s2.setMinutes(Integer.parseInt(tiempos2[1]));
								
				app.getRecurrence().setStart(s);
				app.getRecurrence().setEnd(s2);
				app.getRecurrence().setType(view);
				if(view==0)
				{
					if(opcselect==0)
					{
						app.getRecurrence().getParamsType().put("Every",Integer.parseInt(every1.getText()));
						app.getRecurrence().getParamsType().put("EveryWeekday",false);
					}
					else
					{
						app.getRecurrence().getParamsType().put("Every",false);
						app.getRecurrence().getParamsType().put("EveryWeekday",1);
					}
				}
				else if(view==1)
				{
					app.getRecurrence().getParamsType().put("Recurevery",Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("Sun", weekpatt[0]);
					app.getRecurrence().getParamsType().put("Mon", weekpatt[1]);
					app.getRecurrence().getParamsType().put("Tue", weekpatt[2]);
					app.getRecurrence().getParamsType().put("Wed", weekpatt[3]);
					app.getRecurrence().getParamsType().put("Thu", weekpatt[4]);
					app.getRecurrence().getParamsType().put("Fri", weekpatt[5]);
					app.getRecurrence().getParamsType().put("Sat", weekpatt[6]);
				}
				else if(view==2)
				{
					if(opcselect==0)
					{
						app.getRecurrence().getParamsType().put("day", Integer.parseInt(every1.getText()));
						app.getRecurrence().getParamsType().put("ofEvery", Integer.parseInt(every2.getText()));
						app.getRecurrence().getParamsType().put("The", false);
					}
					else
					{						
						app.getRecurrence().getParamsType().put("The", true);
						app.getRecurrence().getParamsType().put("every", combodia.getSelectedItem());
						app.getRecurrence().getParamsType().put("dayofweek", combotipo.getSelectedItem());
					}
				}
				else
				{
					app.getRecurrence().getParamsType().put("every", Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("onMonth", combodia.getSelectedItem());
					app.getRecurrence().getParamsType().put("dayofmonth", Integer.parseInt(every2.getText()));
				}
				
				app.getRecurrence().setStartDate(fecha.getDate());
				if(statusDate[0])
					app.getRecurrence().setEndDate(null);
				else if(statusDate[1])
				{
					app.getRecurrence().setRecurrenceNum(Integer.parseInt(num2.getText()));
					app.getRecurrence().setEndDate(null);
				}
				else
					app.getRecurrence().setEndDate(fecha2.getDate());
			}
			else
			{
				Task app = (Task)evento;
				Date s = new Date();
				String st = starttime.getSelectedItem().toString();
				String[] tiempos = st.split(":");
				s.setHours(Integer.parseInt(tiempos[0]));
				s.setMinutes(Integer.parseInt(tiempos[1]));
				
				Date s2 = new Date();
				String st2 = endtime.getSelectedItem().toString();
				String[] tiempos2 = st2.split(":");
				s2.setHours(Integer.parseInt(tiempos2[0]));
				s2.setMinutes(Integer.parseInt(tiempos2[1]));
								
				app.getRecurrence().setStart(s);
				app.getRecurrence().setEnd(s2);
				app.getRecurrence().setType(view);
				if(view==0)
				{
					if(opcselect==0)
					{
						app.getRecurrence().getParamsType().put("Every",Integer.parseInt(every1.getText()));
						app.getRecurrence().getParamsType().put("EveryWeekday",false);
					}
					else
					{
						app.getRecurrence().getParamsType().put("Every",false);
						app.getRecurrence().getParamsType().put("EveryWeekday",1);
					}
				}
				else if(view==1)
				{
					app.getRecurrence().getParamsType().put("Recurevery",Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("Sun", weekpatt[0]);
					app.getRecurrence().getParamsType().put("Mon", weekpatt[1]);
					app.getRecurrence().getParamsType().put("Tue", weekpatt[2]);
					app.getRecurrence().getParamsType().put("Wed", weekpatt[3]);
					app.getRecurrence().getParamsType().put("Thu", weekpatt[4]);
					app.getRecurrence().getParamsType().put("Fri", weekpatt[5]);
					app.getRecurrence().getParamsType().put("Sat", weekpatt[6]);
				}
				else if(view==2)
				{
					if(opcselect==0)
					{
						app.getRecurrence().getParamsType().put("day", Integer.parseInt(every1.getText()));
						app.getRecurrence().getParamsType().put("ofEvery", Integer.parseInt(every2.getText()));
						app.getRecurrence().getParamsType().put("The", false);
					}
					else
					{						
						app.getRecurrence().getParamsType().put("The", true);
						app.getRecurrence().getParamsType().put("every", combodia.getSelectedItem());
						app.getRecurrence().getParamsType().put("dayofweek", combotipo.getSelectedItem());
					}
				}
				else
				{
					app.getRecurrence().getParamsType().put("every", Integer.parseInt(every1.getText()));
					app.getRecurrence().getParamsType().put("onMonth", combodia.getSelectedItem());
					app.getRecurrence().getParamsType().put("dayofmonth", Integer.parseInt(every2.getText()));
				}
				
				app.getRecurrence().setStartDate(fecha.getDate());
				if(statusDate[0])
					app.getRecurrence().setEndDate(null);
				else if(statusDate[1])
				{
					app.getRecurrence().setRecurrenceNum(Integer.parseInt(num2.getText()));
					app.getRecurrence().setEndDate(null);
				}
				else
					app.getRecurrence().setEndDate(fecha2.getDate());
			}
				
			
			
		}
		
		for(int k=0; k<btmove.size();k++)
		{
			if(arg0.getActionCommand().equals(btmove.elementAt(k).getActionCommand()))
			{
				opcselect = k;
				if(view==1)
					weekpatt[k] =! weekpatt[k];
				updateOptions();
			}
		}
		
			
		for(int j=0; j<enddate.size();j++)
		{
			if(arg0.getActionCommand().equals(enddate.elementAt(j).getActionCommand()))
			{
				statusDate[j]=!statusDate[j];
				if(statusDate[j])
					enddate.elementAt(j).setIcon(new ImageIcon(dir+"select.png"));
				else
					enddate.elementAt(j).setIcon(new ImageIcon(dir+"noselect.png"));
				
				
				enddate.elementAt(j).updateUI();
			}
		}
		
		for(int i=0;i<btpatter.size();i++)
		{
			if(arg0.getActionCommand().equals(btpatter.get(i).getActionCommand()))
			{
				if(!status[i])
				{
					for(int j2=0;j2<btpatter.size();j2++)
						status[j2]=false;
					status[i]=true;
					changePatter(i);
				}
				else
				{
					status[i]=false;
					status[0]=true;
					changePatter(0);
				}
				updatePatter();
			}
		}
		
	}

	private void updateOptions() 
	{
		for(int i = 0; i<btmove.size();i++)
		{
			if(view==0 || view==2)
			{
				if(opcselect==i)
					btmove.elementAt(i).setIcon(new ImageIcon(dir+"select.png"));
				else
					btmove.elementAt(i).setIcon(new ImageIcon(dir+"noselect.png"));
			}
			else if(view ==1)
			{
				if(weekpatt[i])
					btmove.elementAt(i).setIcon(new ImageIcon(dir+"select.png"));
				else
					btmove.elementAt(i).setIcon(new ImageIcon(dir+"noselect.png"));
			}
			btmove.elementAt(i).updateUI();
		}
	}

}
